﻿#region Copyright
//————————————————————————————————————————//
//	创建人员(Creator)    ：Huangyd
//	创建日期(Date)       ：2012-07-05
//  联系方式(Email)      ：aj-ayumi@163.com; gajayumi@gmail.com; ajayumi@me.com
//  描    述(Description)：
//
//	Copyright (C) 2012 ajayumi 版权所有
//————————————————————————————————————————//
#endregion
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace ajayumi.Platform.Win.UserCtlPanel
{
    /// <summary>
    /// 异步操作的用户空间
    /// </summary>
    public partial class AsyncUserControl : UserControl, IAsyncTaskPattern
    {
        #region Variables

        private CircularProgress.SpinningProgress m_Spg = null;

        #endregion

        public AsyncUserControl()
        {
            InitializeComponent();
            this.InitializeSpg();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            this.AddSpgToParent();
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            this.ChangeSpgLocation();
        }

        protected override void OnHandleDestroyed(EventArgs e)
        {
            this.CancelAsync();
            this.m_Spg.Dispose();
            base.OnHandleDestroyed(e);
        }

        /// <summary>
        /// 将控件添加到父容器中
        /// </summary>
        /// <param name="control"></param>
        private void AddControlToParent(Control control)
        {
            Control parentCon = this.Parent;
            if (parentCon != null)
            {
                parentCon.Controls.Add(control);
                this.m_Spg.BringToFront();
            }
        }

        /// <summary>
        /// 显示调式信息
        /// </summary>
        /// <param name="str">信息字符串</param>
        protected void DisplayDedugMsg(string str)
        { Console.WriteLine(str); }

        /// <summary>
        /// 开始等待
        /// </summary>
        protected virtual void BeginWaiting()
        {
            this.m_Spg.Visible = true;
            this.Enabled = false;
        }

        /// <summary>
        /// 结束等待
        /// </summary>
        protected virtual void EndWaiting()
        {
            this.m_Spg.Visible = false;
            this.Enabled = true;
        }

        /// <summary>
        /// 用于同步模式下，实际处理的操作
        /// </summary>
        protected virtual void DoWork()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 用于异步模式下，实际处理的操作
        /// </summary>
        protected virtual void DoWork(DoWorkEventArgs arg)
        {
            throw new NotImplementedException();
        }

        #region SpinningProgress相关方法

        /// <summary>
        /// 初始化SpinningProgress
        /// </summary>
        private void InitializeSpg()
        {
            this.m_Spg = new CircularProgress.SpinningProgress()
            {
                Size = new Size(50, 50),
                Visible = false
            };
        }
        /// <summary>
        /// 将SpinningProgress添加到父容器中
        /// </summary>
        private void AddSpgToParent()
        {
            this.AddControlToParent(this.m_Spg);
        }
        /// <summary>
        /// 改变SpinningProgress的位置
        /// </summary>
        private void ChangeSpgLocation()
        {
            int left = (this.Width - this.m_Spg.Width) / 2;
            int top = (this.Height - this.m_Spg.Height) / 2;
            this.m_Spg.Location = new Point(left, top);
        }

        #endregion

        #region IAsyncTaskPattern 成员

        public void RunWorkAsync()
        {
            this.BeginWaiting();
            if (!this.bgw.IsBusy)
            {
                this.bgw.RunWorkerAsync();
            }
            //this.bgw.RunWorkerAsync();
        }

        public void CancelAsync()
        {
            this.DisplayDedugMsg(string.Format("{0} 对象：{1} 执行 CancelAsync 方法", DateTime.Now, this.Name));
            this.bgw.CancelAsync();
        }

        public event EventHandler<RunWorkerCompletedEventArgs> RunWorkerCompleted;
        protected virtual void OnRunWorkerCompleted(RunWorkerCompletedEventArgs e)
        {
            if (null != this.RunWorkerCompleted)
            { this.RunWorkerCompleted(this, e); }
        }

        public event EventHandler<ProgressChangedEventArgs> ProgressChanged;

        #endregion

        #region ISyncTaskPattern 成员

        public void RunWork()
        {
            this.DoWork();
        }

        #endregion

        private void bgw_DoWork(object sender, DoWorkEventArgs e)
        {
            this.DisplayDedugMsg(string.Format("{0} 对象：{1} 执行 DoWorkAsync 方法", DateTime.Now, this.Name));

            this.DoWork(e);
        }

        private void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {

        }

        private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (true)
            {
                
            }
            this.OnRunWorkerCompleted(e);
            this.EndWaiting();
        }


    }
}
